//
//  YYEHotCourseTableViewCell.m
//  YuYingEDU
//
//  Created by 胡波 on 2017/12/28.
//  Copyright © 2017年 胡波. All rights reserved.
//

#import "YYEHotCourseTableViewCell.h"
#import "YYEHotCourseCollectionViewCell.h"
#import "YYECoursePlayViewController.h"
#import "YYECourseNoBuyViewController.h"

@interface YYEHotCourseTableViewCell () <UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic,strong) UICollectionView *collectionView;
@property (nonatomic,strong) UICollectionViewFlowLayout *flowLayout;

@end

@implementation YYEHotCourseTableViewCell

static NSString *hotCourseCell = @"hotCourseCell";
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.contentView.backgroundColor = [UIColor whiteColor];
        
        self.flowLayout = [[UICollectionViewFlowLayout alloc]init];
        self.flowLayout.minimumLineSpacing = 0;
        self.flowLayout.minimumInteritemSpacing = 0;
        self.flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        
        self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        self.collectionView.backgroundColor = [UIColor whiteColor];
        self.collectionView.showsHorizontalScrollIndicator = NO;
        self.collectionView.scrollEnabled = NO;
        [self.contentView addSubview:self.collectionView];
        
        [self.collectionView registerClass:[YYEHotCourseCollectionViewCell class] forCellWithReuseIdentifier:hotCourseCell];
        
    }
    return self;
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.collectionView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(UIEdgeInsetsMake(0, 3, 0, 3));
    }];
    
}

#pragma mark --  UICOllectionView
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.datas.count;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    YYEHotCourseCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:hotCourseCell forIndexPath:indexPath];
    
    cell.model = self.datas[indexPath.row];
    
    return cell;
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake(SCREEN_WIDTH / 2 - 3, 71 + (SCREEN_WIDTH / 2 - 6) * 0.55);
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    YYEHomeCourseModel *model = self.datas[indexPath.row];
    NSDictionary *dic = @{
                          @"uid":[LoginModel shareLogin].uid,
                          @"username":[LoginModel shareLogin].username,
                          @"token":[LoginModel shareLogin].token,
                          @"cid":model.cid
                          };
    [SVProgressHUD show];
    [AFNHelper post:LessonAuthority parameters:dic success:^(id responseObject) {
        
        //        NSLog(@"%@",responseObject);
        NSString *isBuy = responseObject[@"data"][@"is_buy"];
        NSString *isCollect = [responseObject[@"data"][@"is_collect"] stringValue];
        NSString *mid = responseObject[@"data"][@"mid"];
        NSString *playback = responseObject[@"data"][@"playback"];
        NSString *mid2 = responseObject[@"data"][@"mid2"];
        NSString *playback2 = responseObject[@"data"][@"playback2"];
        
        YYECoursePlayViewController *vc = [[YYECoursePlayViewController alloc]init];
        vc.cid = model.cid;
        vc.isBuy = isBuy;
        vc.isCollect = isCollect;
        vc.mid = mid;
        vc.playback = playback;
        vc.mid2 = mid2;
        vc.playback2 = playback2;
        [[self viewController].navigationController pushViewController:vc animated:YES];
        
        
        [SVProgressHUD showWithStatus:responseObject[@"message"]];
        [SVProgressHUD dismiss];
        
    } faliure:^(id error) {
        
        NSLog(@"请求失败");
        [SVProgressHUD dismiss];
        
    }];
    
}

- (UIViewController *)viewController {
    
    for (UIView *next = [self superview]; next; next = next.superview) {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
    
}

- (void)setDatas:(NSArray *)datas {
    _datas = datas;
    
    [self.collectionView reloadData];
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
